﻿using System.Collections.Generic;
using System.Web.Mvc;
using Century.Web.UI.MVC;
using Century.Web.Security.Attributes;
using Century.EnterpriseLibrary.Globalization;
using Century.Model.CMS;
using Century.Business.CMS;

namespace Century.Admin.Controllers
{
    [Authorization(Group = "", Name = "InlineEditing")]
    public class InlineEditingController : CenturyAdminBaseController
    {
        public InlineEditingController(ISiteService siteService) : base(siteService) { }

        #region [ Label ]

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [Authorization(Group = "", Name = "InlineEditing")]
        public string UpdateLabel(string key, string value, string countrySiteId)
        {
            if (string.IsNullOrEmpty(countrySiteId))
            {
                countrySiteId = Token.CurrentSite.Id.ToString();
            }
            System.Web.HttpContext.Current.Session["CurrentSiteId"] = int.Parse(countrySiteId);
            GlobalizationManager.CMSUpdateTranslation(string.Empty, key, value, Token.CurrentSite.Id);
            return value;
        }

        #endregion

        #region [ Position ]

        public bool ChangeOrder(int pageId, int holderId, Dictionary<int, int> partId_orderId)
        {
            return true;
        }


        #region [ Html Position ]

        public JsonResult AddStaticContent(int pageId, int holderId)
        {
            ContentHolderPart part = siteService.AddStaticContent(pageId, holderId);
            return Json(new { key = part.StaticContentId, ordid = part.PartOrder, partid = part.Id });
        }


        public bool RemovePart(int pageId, int partId)
        {
            return siteService.RemovePart(pageId, partId);
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateInput(false)]
        [Authorization(Group = "", Name = "InlineEditing")]
        public string UpdateStaticContent(string key, string value, string pageId)
        {
            siteService.UpdateStaticContent(int.Parse(key), value, int.Parse(pageId));
            return value;
        }

        #endregion

        #region [ View Position ]

        public ActionResult DynamicContentSelector(int pageId, int holderId)
        {
            ViewBag.HolderId = holderId;
            ViewBag.PageId = pageId;
            List<DynamicContent> dynamicContents = siteService.GetDynamicContents();
            return View(dynamicContents);
        }

        //[HttpPost]
        //public bool DynamicContentSelect(int id, int holderId, int pageId)
        //{
        //    using (DynamicContentBSO db = new DynamicContentBSO())
        //    {
        //        return db.AddViewToPosition(id, holderId, pageId);
        //    }
        //}

        //public JsonResult AddDynamicContent(int pageId, int holderId, int viewId)
        //{

        //    ContentHolderPart part = PersistDataUpdater.AddDymamicContent(pageId, holderId, viewId);
        //   // var v = new ViewContext();
        //   //var h = new HtmlHelper(v, new ViewPage());
        //    //ControllerContext.HttpContext
        //   //ControllerContext, part.DynamicContent.ActionName, part.DynamicContent.ControllerName, new { area = part.DynamicContent.AreaName });
        //   //PartialRequest pr = new PartialRequest(new
        //   // {
        //   //     controller = part.DynamicContent.ControllerName,
        //   //     action = part.DynamicContent.ActionName,
        //   //     area = part.DynamicContent.AreaName
        //   // });

        //    return Json(new
        //    {
        //        key = part.DynamicContentId,
        //        ordid = part.PartOrder,
        //        partid = part.Id
        //        ,
        //        view = part.DynamicContent.AreaName + "/" + part.DynamicContent.ControllerName + "/" + part.DynamicContent.ActionName
        //    });
        //}

        #endregion

        #endregion

        public ActionResult InlineEditingHeaderScripts()
        {
            return View();
        }

        public ActionResult InlineEditingBodyHtml()
        {
            return View();
        }
    }

    //public class PartialRequest
    //{
    //    public RouteValueDictionary RouteValues { get; private set; }

    //    public PartialRequest(object routeValues)
    //    {
    //        RouteValues = new RouteValueDictionary(routeValues);
    //    }

    //    public void Invoke(ControllerContext context)
    //    {
    //        RouteData rd = new RouteData(context.RouteData.Route, context.RouteData.RouteHandler);
    //        foreach (var pair in RouteValues)
    //            rd.Values.Add(pair.Key, pair.Value);
    //        IHttpHandler handler = new MvcHandler(new RequestContext(context.HttpContext, rd));
    //        handler.ProcessRequest(System.Web.HttpContext.Current);
    //    }
    //}
}
